#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/1/14 14:52
@desc
"""

import time
import hashlib
import urllib2
import logging
from pyquery import PyQuery as pyq


class WeiXin:

    def __init__(self):
        pass

    def md5(self, str):
        m = hashlib.md5()
        m.update(str)
        return m.hexdigest()

    def get_date(self):
        t = time.time()
        now_time = lambda: int(round(t * 1000))
        today = time.strftime("%Y-%m-%d", time.localtime(now_time() / 1000))
        return today

    def getUrl_multiTry(self, url):
        global html
        user_agent = '"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36"'
        headers = {'User-Agent': user_agent}
        maxTryNum = 10
        for tries in range(maxTryNum):
            try:
                req = urllib2.Request(url, headers=headers)
                html = urllib2.urlopen(req).read()
                break
            except:
                if tries < (maxTryNum - 1):
                    continue
                else:
                    logging.error("Has tried %d times to access url %s, all failed!", maxTryNum, url)
                    break

        return html

    def get_info_from_weixin(self, url):
        html = self.getUrl_multiTry(url)
        py_html = pyq(html)
        mp_id = self.md5(url)
        title = py_html('#img-content>h2').text().replace(u'\xa0', '').replace(u'\xb2', '')
        weixin_mp_name = py_html('#meta_content>span>a').text().replace(u'\xa0', '').replace(u'\xb2', '')
        content = py_html('#js_content').text().replace(u'\xa0', '')
        content = content.replace('\n', '')
        data = {'_id': mp_id, 'weixin_mp_name': weixin_mp_name, 'title': title, 'content': content,
                'extract_time': self.get_date()}
        return data


if __name__ == '__main__':

    url = 'https://mp.weixin.qq.com/s/2pNtVrAsr8-vjohqFbT5tA'
    wx = WeiXin()
    data = wx.get_info_from_weixin(url)
    print(data['content'])
